package com.dxy.hms.controller;

import cn.hutool.core.map.MapUtil;
import cn.hutool.json.JSONUtil;
import lombok.Data;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.Instant;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * 临时写在这里的其他项目的测试代码
 */
@RestController
@RequestMapping("/hms/ptkjTest")
public class PtkjTestController {

    @PostMapping("/api/oauth/token")
    public ResponseEntity<Object> auth(LoginParam param) throws Exception{
        System.out.println("auth-param: " + param);
        Map<Object, Object> responseBody = MapUtil.builder()
                .put("access_token", "bda5967a6dcb459fb703e6babbf0248788aeb9dc")
                .put("expires_in", 7200)
                .put("token_type", "Bearer")
                .put("scope", "all")
                .build();
        // 设置响应头
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.TEXT_HTML);

        TimeUnit.SECONDS.sleep(4);
        // 返回 ResponseEntity
        return new ResponseEntity<>(JSONUtil.toJsonStr(responseBody), headers, HttpStatus.OK);
    }

    @PostMapping("/api/iot/manage/indicatorInfo")
    public Object getTargetInfo(@RequestHeader("Authorization") String token,
                                @RequestBody GetTargetParam param) throws Exception {
        System.out.println("getTargetInfo-token: " + token);
        System.out.println("getTargetInfo-param: " + param);
        TimeUnit.SECONDS.sleep(4);
        return MapUtil.builder()
                .put("code", 1)
                .put("msg", "ok")
                .put("time", String.valueOf(Instant.now().getEpochSecond()))
                .put("data", Arrays.asList(
                        MapUtil.builder()
                                .put("target_code", "44010013_WX01_G92_WD01")
                                .put("target_name", "温度")
                                .put("equip_code", "44010013_WX01_G92")
                                .put("threshold_down2", -10.00)
                                .put("threshold_down", -5.00)
                                .put("threshold_up", 100.00)
                                .put("threshold_up2", 150.00)
                                .put("range_up", 200.00)
                                .put("range_down", -15.00)
                                .put("unit", "℃")
                                .put("description", "拱顶氮封罐")
                                .build()
                ))
                .build();
    }

    @PostMapping("/api/iot/report")
    public Object report(@RequestHeader("Authorization") String token,
                         @RequestBody ReportDataParam param) {
        System.out.println("report-token: " + token);
        System.out.println("report-param: " + param);
        return MapUtil.builder()
                .put("code", 1)
                .put("msg", "ok")
                .put("time", String.valueOf(Instant.now().getEpochSecond()))
                .put("data", null)
                .build();
    }

    @Data
    static class LoginParam {
        private String client_id;
        private String client_secret;
        private String grant_type;
        private String scope;
    }

    @Data
    static class GetTargetParam {
        private String credit_code;
        private String gatewayId;
        private String equip_code;
        private String targetCode;
    }

    @Data
    static class TargetData {
        private String addr;
        private Integer addrv;
    }

    @Data
    static class ReportDataParam {
        private String credit_code;
        private String gatewayId;
        private String dataId;
        private String collectTime;
        private String reportType;
        private List<TargetData> datas;
    }

}
